﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace myMoney.SystemManage
{
    public partial class ucBank : UserControl
    {
        public ucBank()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            loadData();
        }

        #region Method

        private void loadData()
        {
            this.lvBank.Items.Clear();

            DataTable dt = MoneyClass.Bank.GetAll();

            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.SubItems[0].Text = (this.lvBank.Items.Count + 1).ToString();
                item.SubItems.Add(dr["BankName"].ToString());
                item.Tag = dr["guid"].ToString();

                this.lvBank.Items.Add(item);
            }

            dt.Dispose();
            this.tsslMsg.Text = string.Format("共{0}条记录", this.lvBank.Items.Count);
        }

        #endregion

        private void lvBank_Click(object sender, EventArgs e)
        {
            this.rbtnUpdate.Checked = true;
            this.tbBankName.Text = this.lvBank.SelectedItems[0].SubItems[1].Text;
            this.pTop.Tag = this.lvBank.SelectedItems[0].Tag;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (this.tbBankName.Text.Length == 0)
            {
                MessageBox.Show("请输入银行名称");
                return;
            }

            int rval = 0;
            if (this.rbtnAdd.Checked) rval = MoneyClass.Bank.Add(this.tbBankName.Text);
            if (this.rbtnUpdate.Checked) rval = MoneyClass.Bank.Update(this.pTop.Tag.ToString(), this.tbBankName.Text);

            switch (rval)
            {
                case -2:
                    MessageBox.Show("名称已存在");
                    break;
                case -1:
                case 0:
                    MessageBox.Show("操作失败");
                    break;
                case 1:
                    MessageBox.Show("添加成功");
                    loadData();
                    break;
                default:
                    MessageBox.Show("数据异常");
                    break;
                    
            }
        }
    }
}
